/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * actualizarPeli.java
 *
 * Created on 12/07/2011, 02:46:37 PM
 */
package mongolo;

import Servicios.*;
import Biblioteca.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Pablo.Montero
 */
public class actualizarPeli extends javax.swing.JFrame {

    /** Creates new form actualizarPeli */
    public actualizarPeli() {
        initComponents();
        this.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        actualizarPeli_NombrePeli = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        actualizarPeli_Ano = new javax.swing.JTextField();
        actualizarPeli_BTN = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        actualizarPeli_CantPelisDisponibles = new javax.swing.JTextField();
        actualizarPeli_CodigoPeli = new javax.swing.JComboBox();
        eliminarPeli_BTN = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setText("Codigo");

        jLabel2.setText("Nombre de la pelicula");

        actualizarPeli_BTN.setLabel("Actualizar pelicula");
        actualizarPeli_BTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actualizarPeli_BTNActionPerformed(evt);
            }
        });

        jLabel3.setText("Año");

        jLabel4.setText("Cantidad peliculas disponibles");

        actualizarPeli_CodigoPeli.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                actualizarPeli_CodigoPeliItemStateChanged(evt);
            }
        });

        eliminarPeli_BTN.setText("Eliminar Pelicula");
        eliminarPeli_BTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarPeli_BTNActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(actualizarPeli_BTN)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(actualizarPeli_CodigoPeli, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(actualizarPeli_CantPelisDisponibles, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(actualizarPeli_Ano, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(actualizarPeli_NombrePeli, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
                    .addComponent(eliminarPeli_BTN))
                .addContainerGap(53, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actualizarPeli_CodigoPeli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(actualizarPeli_NombrePeli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actualizarPeli_Ano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actualizarPeli_CantPelisDisponibles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actualizarPeli_BTN)
                    .addComponent(eliminarPeli_BTN))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private boolean notYet = true;
    IMapeoObjetos mapo = new MapeoObjetos();

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        if (notYet) {
            notYet = false;
            ArrayList<Peli> LisPeli = mapo.getPeli();
            actualizarPeli_CodigoPeli.addItem("");
            for (Peli peli : LisPeli) {
                actualizarPeli_CodigoPeli.addItem(peli.id);
            }
        }

    }//GEN-LAST:event_formWindowActivated

    private void actualizarPeli_BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actualizarPeli_BTNActionPerformed
        String codiPel = SM.ValidarNulls(actualizarPeli_CodigoPeli.getSelectedItem(), "Error");
        String nomPel = SM.ValidarNulls(actualizarPeli_NombrePeli.getText(), "Error");
        String ano = SM.ValidarNulls(actualizarPeli_Ano.getText(), "Error");
        int caPD = SM.ValidarInts(actualizarPeli_CantPelisDisponibles.getText());

        if (caPD > 0) {
            if (mapo.UpdatePeli(codiPel, nomPel, ano, caPD)) 
                JOptionPane.showMessageDialog(null, "Se actualizo la peli");            
            else
                JOptionPane.showMessageDialog(null, SM.lastError);
        } else {
            JOptionPane.showMessageDialog(null, "Error en la casilla cantidad de peliculas");
        }
}//GEN-LAST:event_actualizarPeli_BTNActionPerformed

    private void actualizarPeli_CodigoPeliItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_actualizarPeli_CodigoPeliItemStateChanged
        String aa = SM.ValidarNulls(actualizarPeli_CodigoPeli.getSelectedItem(),"ERROR");

        if (!" ".equals(aa)) {
            ArrayList<Peli> LisPeli = mapo.getPeli(aa);
            for (Peli p : LisPeli) {
                this.actualizarPeli_NombrePeli.setText(p.nom);
                this.actualizarPeli_Ano.setText(p.ano);
                this.actualizarPeli_CantPelisDisponibles.setText(SM.ValidarString(p.cantTotalPelis));
            }
        }
    }//GEN-LAST:event_actualizarPeli_CodigoPeliItemStateChanged

    private void eliminarPeli_BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarPeli_BTNActionPerformed
        String aa = SM.ValidarNulls(actualizarPeli_CodigoPeli.getSelectedItem(), "Error");
        if (SM.cMes("Esta seguro que desea eliminar a pelicula")) {
            if (!" ".equals(aa)) {
               if(mapo.DeletePeli(aa))
                   JOptionPane.showMessageDialog(null, "se eleimno correctamente");
               else
                   JOptionPane.showMessageDialog(null, SM.lastError);            
            }
        }
    }//GEN-LAST:event_eliminarPeli_BTNActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField actualizarPeli_Ano;
    private javax.swing.JButton actualizarPeli_BTN;
    private javax.swing.JTextField actualizarPeli_CantPelisDisponibles;
    private javax.swing.JComboBox actualizarPeli_CodigoPeli;
    private javax.swing.JTextField actualizarPeli_NombrePeli;
    private javax.swing.JButton eliminarPeli_BTN;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables
}
